﻿using HCMS.CommonDtos;
using HCMS.Dtos.Manage;
using HCMS.Entities.Manage;
using HCMS.Extensions;
using HCMS.IRepositories.Manage;
using HCMS.IServices.Manage;
using HCMS.Repositories.Manage;
using HCMS.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace HCMS.Services.Manage
{
    /// <summary>
    /// 管理员 操作日志 应用服务
    /// </summary>
    public class AdminActionLogsAppService : CommonAppService<ManageDbContext, AdminActionLogs>, IAdminActionLogsAppService
    {
        private readonly IRepository<AdminActionLogs> baseAdminActionLogsRepository;
        private readonly IAdminActionLogsRepository adminActionLogsRepository;
        private readonly IAdminRepository adminRepository;

        public AdminActionLogsAppService(
            IRepository<AdminActionLogs> baseAdminActionLogsRepository,
            IAdminActionLogsRepository adminActionLogsRepository,
            IAdminRepository adminRepository
            ) : base(baseAdminActionLogsRepository)
        {
            this.baseAdminActionLogsRepository = baseAdminActionLogsRepository;
            this.adminActionLogsRepository = adminActionLogsRepository;
            this.adminRepository = adminRepository;
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<AdminActionLogsListDto>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            if (!string.IsNullOrWhiteSpace(searchArgs.SearchName) && !string.IsNullOrWhiteSpace(searchArgs.SearchKey))
            {
                // 【管理员 姓名】转为【管理员 Id】搜索
                if (searchArgs.SearchName.IsSame("adminName"))
                {
                    searchArgs.SearchName = "ids";
                    var adminIdList = (await adminRepository.GetListAsync()).Where(t => t.AdminName.Contains(searchArgs.SearchKey))?.Select(t => t.Id).ToList();
                    if (adminIdList != null && adminIdList.Any())
                    {
                        searchArgs.SearchKey = string.Join(",", adminIdList);
                    }
                    else
                    {
                        searchArgs.SearchKey = "";
                    }
                }
            }

            var logs = await adminActionLogsRepository.ManageList(searchArgs, pager);

            var result = new ListPagerDto<AdminActionLogsListDto>();
            result.PagerInfo = logs.PagerInfo;

            var data = new List<AdminActionLogsListDto>();
            foreach (var item in logs.Data)
            {
                // 关联 管理员 名称
                var adminName = string.Empty;
                var adminInfo = await adminRepository.FirstOrDefaultAsync(t => t.Id == item.AdminId);
                if (adminInfo != null)
                {
                    adminName = adminInfo.AdminName;
                }

                data.Add(new AdminActionLogsListDto
                {
                    Id = item.Id,
                    CreateTime = item.CreateTime,
                    AdminId = item.AdminId,
                    AdminName = adminName,
                    ControllerName = item.ControllerName,
                    ActionName = item.ActionName,
                    ExecutionTime = DateTimeUtil.ExecutionTimeStr(item.ExecutionTime),
                    Url = item.Url,
                    Ip = item.Ip
                });
            }

            result.Data = data;

            return result;
        }

    }
}
